package top.zy.service.zymallgoodsservice.brand.service;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zy.common.zymallcommon.utils.constants.CacheConstants;
import top.zy.common.zymallcommon.utils.exception.ZymalException;
import top.zy.common.zymallcommon.vo.PageResult;
import top.zy.service.zymallgoodsinterface.dto.BrandPageParam;
import top.zy.service.zymallgoodsinterface.entity.Brand;
import top.zy.service.zymallgoodsinterface.entity.Category;
import top.zy.service.zymallgoodsservice.brand.dao.BrandMapper;
import top.zy.service.zymallgoodsservice.category.dao.CategoryMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 10:35 2019/6/20
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public PageResult<Brand> queryBrandPage(BrandPageParam brandPageParam) {
        String desc="ASC";
        if (brandPageParam.getPage()==null){
              brandPageParam.setPage(1);
        }
        if (brandPageParam.getRows()==null){
              brandPageParam.setRows(5);
        }
     if (brandPageParam.getSearch()!=null){
            brandPageParam.setSearch(brandPageParam.getSearch().toUpperCase());
     }
    if (brandPageParam.getSortBy()!=null){
        if (brandPageParam.getDesc()){
            desc="DESC";
        }
    }
       PageHelper.startPage(brandPageParam.getPage(),brandPageParam.getRows());

       List<Brand> brandList=brandMapper.selectPageBySearch(brandPageParam.getSearch(),brandPageParam.getSortBy(),desc.replaceAll(" ", ""));
       PageResult<Brand> pageResult=new PageResult<>();
       int count=brandMapper.selectAllCount(brandPageParam.getSearch(),brandPageParam.getSortBy(),desc.replaceAll(" ", ""));
       pageResult.setTotal(Long.valueOf(count));
       pageResult.setItems(brandList);
       return pageResult;
    }

    @Override
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
     int result=brandMapper.insert(brand);
     if (result!=1){
         throw new ZymalException("");
     }
        for (Long cid:cids) {
           result=brandMapper.insertBrandCategory(brand.getId(),cid);
            if (result!=1){
                throw new ZymalException("");
            }
        }
    }

    @Cacheable(value = CacheConstants.CACHE_GOODS_BRAND_CATEGORYID,key = "#p0")
    @Override
    public List<Brand> queryBrandListByCategoryId(Long id) {
        List<Brand> brandList=new ArrayList<>();
      List<Long> brandIdList=brandMapper.selectBrandIdByCategoryId(id);
      for (Long bid:brandIdList){
         Brand brand=brandMapper.selectByPrimaryKey(bid);
         brandList.add(brand);
      }
        return brandList;
    }

    @Cacheable(value = CacheConstants.CACHE_GOODS_BRAND,key = "#p0")
    @Override
    public Brand queryBrandById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Cacheable(value = CacheConstants.CACHE_GOODS_BRAND_IDS)
    @Override
    public List<Brand> queryBrandByIds(List<Long> ids) {
        return brandMapper.queryBrandByIds(ids);
    }
}
